import subprocess
from IPy import IP
ip = IP("192.168.0.0/24")
for i in ip:
    # subprocess.call返回命令的结果和执行状态
    result = subprocess.call(f"ping -c 1 {i} ", shell=True, stdout=subprocess.PIPE)
    # 返回结果为0代表ping通了，非0就是没有ping通
    if result == 0:
        out = f"{i}已经被使用" + "\n"
    else:
        out = f"{i}没有被使用" + "\n"
    # 将输出结果写入文件里面
    with open("scan_ip.txt", "a", encoding="utf-8") as fe:
        fe.write(out)

